package cn.tedu.hospitalgroupproject.server.content.controller;

import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.JsonResult;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorListItemVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDoctorService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 处理医生相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/management/doctors/")
@Api(tags="1.1 管理部分-医生管理")
public class DoctorController {

    @Autowired
    private IDoctorService doctorService;

    public DoctorController() {
        log.info("创建控制器对象:DoctorController");
    }

    @ApiOperation("添加医生")
    @ApiOperationSupport(order = 150)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    @PostMapping("/add-new")
    public JsonResult addNew(@Validated DoctorAddNewParam doctorAddNewParam) {
        log.debug("开始处理【新增医生数据】的请求，添加类：{}", doctorAddNewParam);
        doctorService.addNew(doctorAddNewParam);
        return JsonResult.ok();
    }


    @ApiOperation("删除医生数据")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "删除医生数据失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【删除指定ID下医生数据】的请求，参数：{}", id);
        doctorService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("开启管理员权限")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/isAdmin")
    public JsonResult setIsAdmin(@PathVariable @Range(min = 1, message = "开启管理员权限失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【开启管理员权限】的请求，参数：{}", id);
        doctorService.setIsAdmin(id);
        return JsonResult.ok();
    }

    @ApiOperation("关闭管理员权限")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/isNotAdmin")
    public JsonResult setIsNotAdmin(@PathVariable @Range(min = 1, message = "关闭管理员权限失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【关闭管理员权限】的请求，参数：{}", id);
        doctorService.setIsNotAdmin(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改医生信息")
    @ApiOperationSupport(order = 350)
    @PostMapping("/{id:[0-9]+}/update/info")
    public JsonResult updateInfoById(@Validated DoctorUpdateInfoParam doctorUpdateInfoParam) {
        log.debug("开始处理【修改医生信息】的请求，参数：{}", doctorUpdateInfoParam);
        doctorService.updateInfoById(doctorUpdateInfoParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID查询医生信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "获取医生信息失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询标签】的请求，参数：{}", id);
        DoctorStandardVO doctorStandardVO = doctorService.getStandardById(id);
        return JsonResult.ok(doctorStandardVO);
    }

    @ApiOperation("查询医生列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/list")
    public JsonResult listDoctor(Integer page, String queryType) {
        log.debug("开始处理【查询医生列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<DoctorListItemVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = doctorService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = doctorService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }



}
